<template>
  <div>
    <div>
      <span>编号 </span>
      <el-select v-model="id" placeholder="商铺完整编号">
        <el-option
          v-for="(item, index) in listId"
          :key="index"
          :label="item"
          :value="item"
        >
        </el-option>
      </el-select>
      <span>编号 </span>
      <el-select v-model="id" placeholder="商铺完整编号">
        <el-option
          v-for="(item, index) in listId"
          :key="index"
          :label="item"
          :value="item"
        >
        </el-option>
      </el-select>
    </div>
    
    <div style="margin-top:15px">
      <el-button type="primary" icon="el-icon-search">搜索</el-button>
      <el-button type="primary"  @click="visible=true">上传<i class="el-icon-upload el-icon--right"></i></el-button>
    </div>
    <div style="margin-top:15px">
      <el-table :data="tableData" border style="width: 100%">
        <el-table-column  type="index" label="序号" ></el-table-column>
        <el-table-column prop="name" label="完整编号"> </el-table-column>
        <el-table-column prop="block" label="商业区" ></el-table-column>
        <el-table-column prop="building" label="楼栋"> </el-table-column> 
        <el-table-column prop="floor" label="楼层" > </el-table-column>
        <el-table-column prop="name" label="房号"></el-table-column>
        <el-table-column prop="buildingSquare" label="占地面积"> </el-table-column> 
          <el-table-column prop="useSquare" label="使用面积" > </el-table-column>
        <el-table-column prop="rentFee" label="租金" ></el-table-column>
        <el-table-column prop="state" label="使用状态"> </el-table-column> 
         <el-table-column prop="address" label="操作"> 
            <el-button type="text">编辑</el-button>
            <el-button type="text">删除</el-button>
         </el-table-column> 
     </el-table>
    </div>

    <el-dialog title="批量导入" :visible.sync="visible">
        <el-form
          :model="ruleForm1"
          :rules="rules"
          ref="ruleForm"
          label-width="100px"
          class="demo-ruleForm"
        >
        <el-steps :active="1">
            <el-step title="步骤 1"  description="上传文件"   icon="el-icon-edit">  </el-step>

            <el-step title="步骤 2" description="导入结果"  icon="el-icon-upload"></el-step>
        </el-steps>
      <div >
        
        <el-upload class="upload-demo" multiple action="http://localhost:8080/upload">
            <div class="upload-inner" style="border: 1px solid #000; padding: 10px;" >
              <i class="el-icon-upload" style="width: 100px; height: 100px;" ></i>
              <div class="upload-info">
                  <h3>上传填好的数据表</h3>
                  <p upload-text>文件的后缀名必须是xls或xlsx,文件大小不超过500kb,最多支持导入1000条数据</p>
                  <el-button type="text" >上传文件</el-button>
              </div>
            </div>
         </el-upload>

            <div class="upload-inner" style="border: 1px solid #000; padding: 10px;">
              <i class="el-icon-upload" style="width: 100px; height: 100px;"></i>
              <div class="upload-info">
                <h3>填写导入的数据信息</h3>
                <br> <br>
                <p>请按照数据模板的格式准备导入数据，模板中的表头名称不可更改，表头行不能删除</p>
                <el-button type="text" @click="downloadFileTemplate">下载数据模板</el-button>
              </div>
            </div>
      
        </div>

              <el-form-item style="margin-left: 300px">
                <el-button type="primary" @click="visible=false">取消</el-button >
                <el-button @click="submitForm1">确定</el-button>
              </el-form-item> 
        </el-form>
      </el-dialog>

    <div>
        <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="currentPage"
      :page-sizes="[5, 10, 15, 20]"
      :page-size="100"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total">
    </el-pagination>
    </div>

  </div>
</template>

<script>
export default {
  name: "shopManagement",


   data() {
    return {
      listId: [],
      id: "",
      tableData:[],

      currentPage: 1,
      pageSize: 5,
      total: 0,
      visible:false,

    };
  },
  methods: {
     downloadFileTemplate() {
      this.$http
        .get("http://localhost:8080/download", {
          responseType: "blob",
        })
        .then((response) => {
          // 创建一个URL对象
          const url = window.URL.createObjectURL(new Blob([response.data]));
          // 创建一个<a>标签，设置其href属性为下载的URL
          const link = document.createElement("a");
          link.href = url;
          // 设置下载文件的名称
          link.setAttribute("download", "商铺导入模板.xlsx");
          // 将<a>标签添加到文档中
          document.body.appendChild(link);
          // 模拟点击<a>标签来触发文件下载
          link.click();
          // 完成后，清除URL对象和<a>标签
          window.URL.revokeObjectURL(url);
          document.body.removeChild(link);
        })
        .catch((err) => {
          this.$message.error(err);
        });
    },


      handleUpload() {
        var formData = new FormData();
        formData.append('file', file);
        
        fetch("http://localhost:8080/upload", {
          method: 'POST',
          body: formData
        })
        .then(response => response.json())
        .then(data => {
          // 处理后端返回的响应数据
          console.log(data);
        })
        .catch(error => {
          console.error('上传文件出错:', error);
        });
       
      },
     handleSizeChange(pageSize) {
         this.shopManageMentList(this.currentPage, pageSize);
      },
      handleCurrentChange(pageNum) {
       this.shopManageMentList(pageNum, this.pageSize);
      },

      shopManageMentList(
      pageNum,
      pageSize,
    ) {
      this.$http
        .get("http://localhost:8080/selectShopMessageList", {
          params: {
            pageNum: pageNum,
            pageSize: pageSize,
          },
        })
        .then((result) => {
          let $data = result.data; //表格数据
          this.tableData = $data.data.list; //当前页
          this.currentPage = $data.data.pageNum; //每页显示条数
          this.pageSize = $data.data.pageSize; //每页显示条数
          this.total = $data.data.total; //总条数
        })
        .catch((err) => {
          this.$message.error(err);
        });
    },


    
  },

  mounted() {
        this.shopManageMentList(this.currentPage, this.pageSize);
    
  },
};
</script>

<style>
.upload-inner {
  display: flex;
  align-items: center;
}

.upload-info {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  margin-left: 10px;
}

.upload-info h3 {
  text-align: left;
  margin: 0;
}
.upload-info p {
  text-align: left;
  margin: 0;
}
.upload-text {
  white-space: nowrap;
}
</style>